_BUILDTIME_OSPL_HOME=@OpenSplice_HOME@

# Give priority to the OpenSplice installation found when the workspace was built, if it's valid.
# If it's not valid, the current value of the OSPL_HOME environment variable will be used.
# The one detected at buildtime may refer to an invalid path for users using "fat archive" binaries, for example.
if [ -d "$_BUILDTIME_OSPL_HOME" ]; then
  # Warn about unused environment variable.
  if [ ! -z "$OSPL_HOME" -a "$OSPL_HOME" != "$_BUILDTIME_OSPL_HOME" ]; then
    echo "[opensplice_cmake_module] Warning: OSPL_HOME environment variable is set to [[$OSPL_HOME]]." \
      "When the workspace was built, OpenSplice was found at [[$_BUILDTIME_OSPL_HOME]]," \
      "which is what will actually be used." \
      "Manually modify the environment now if this is not the configuration you want." 1>&2
  fi
  _OSPL_HOME_TO_USE=$_BUILDTIME_OSPL_HOME
else
  if [ -z "$OSPL_HOME" ]; then
    echo "[opensplice_cmake_module] Warning:"\
      "The location at which OpenSplice was found when the workspace was built [[$_BUILDTIME_OSPL_HOME]]" \
      "does not point to a valid directory, and the OSPL_HOME environment variable has not been set." \
      "Support for OpenSplice will not be available." 1>&2
    unset _BUILDTIME_OSPL_HOME
    return
  fi

  if [ ! -d "$OSPL_HOME" ]; then
    echo "[opensplice_cmake_module] Warning:" \
      "Neither the location at which OpenSplice was found when the workspace was built [[$_BUILDTIME_OSPL_HOME]]" \
      "nor the OSPL_HOME environment variable [[$OSPL_HOME]] point to a valid directory." \
      "Support for OpenSplice will not be available." 1>&2
    unset _BUILDTIME_OSPL_HOME
    return
  fi
  _OSPL_HOME_TO_USE=$OSPL_HOME
fi

# TODO(jacobperron): Find a better solution that does not rely on colcon
# if AMENT_CURRENT_PREFIX is not set, try to use COLCON_CURRENT_PREFIX if available
if [ ! -d "$AMENT_CURRENT_PREFIX" ]; then
  if [ -z "$COLCON_CURRENT_PREFIX" ]; then
    echo "The compile time prefix path '$AMENT_CURRENT_PREFIX' doesn't " \
      "exist. Consider sourcing a different extension than '.sh'." 1>&2
  else
    AMENT_CURRENT_PREFIX="$COLCON_CURRENT_PREFIX"
  fi
fi

_DEFAULT_OSPL_URI="file://$AMENT_CURRENT_PREFIX/share/opensplice_cmake_module/config/ros_ospl.xml"

if [ -f "$_OSPL_HOME_TO_USE/release.com" ]; then
  _ORIGINAL_OSPL_URI="$OSPL_URI"
  . "$_OSPL_HOME_TO_USE/release.com" > /dev/null
  if [ -n "$_ORIGINAL_OSPL_URI" ]; then
    # If the OSPL_URI was set before sourcing the release.com script, restore it.
    export OSPL_URI="$_ORIGINAL_OSPL_URI"
  else
    # Otherwise override the OSPL_URI from the release.com with the ros default config.
    export OSPL_URI="$_DEFAULT_OSPL_URI"
  fi
  unset _ORIGINAL_OSPL_URI
else
  export OSPL_TMPL_PATH=$_OSPL_HOME_TO_USE/etc/opensplice/idlpp

  # function to prepend non-duplicate values to environment variables
  # using colons as separators and avoiding trailing separators
  # copied from https://github.com/ament/ament_package/blob/6c8acd25498d2674e7ad9d316b35d112ae1ab8c1/ament_package/template/package_level/local_setup.sh.in#L45-L94
  # since this script can't assume that the package level script was sourced before
  _opensplice_prepend_unique_value() {
    # arguments
    _listname="$1"
    _value="$2"
    #echo "listname $_listname"
    #eval echo "list value \$$_listname"
    #echo "value $_value"

    # check if the list contains the value
    eval _values=\"\$$_listname\"
    _duplicate=
    _connext_prepend_unique_value_IFS=$IFS
    IFS=":"
    if [ "$AMENT_SHELL" = "zsh" ]; then
      # convert array-like string into array
      # to wordaround SH_WORD_SPLIT not being set
      eval _values="(\"${=_values}\")"
    fi
    for _item in $_values; do
      # ignore empty strings
      if [ -z "$_item" ]; then
        continue
      fi
      if [ "$_item" = "$_value" ]; then
        _duplicate=1
      fi
    done
    unset _item

    # prepend only non-duplicates
    if [ -z "$_duplicate" ]; then
      # avoid trailing separator
      if [ -z "$_values" ]; then
        eval export $_listname=\"$_value\"
        #eval echo "set list \$$_listname"
      else
        # field separator must not be a colon
        unset IFS
        eval export $_listname=\"$_value:\$$_listname\"
        #eval echo "prepend list \$$_listname"
      fi
    fi
    IFS=$_connext_prepend_unique_value_IFS
    unset _connext_prepend_unique_value_IFS
    unset _duplicate
    unset _values

    unset _value
    unset _listname
  }

  # detect if running on Darwin platform
  _UNAME=`uname -s`
  _IS_DARWIN=0
  if [ "$_UNAME" = "Darwin" ]; then
    _IS_DARWIN=1
  fi
  unset _UNAME

  if [ $_IS_DARWIN -eq 0 ]; then
    _opensplice_prepend_unique_value LD_LIBRARY_PATH "$_OSPL_HOME_TO_USE/lib"
  else
    _opensplice_prepend_unique_value DYLD_LIBRARY_PATH "$_OSPL_HOME_TO_USE/lib"
  fi
  unset _IS_DARWIN
  unset _opensplice_prepend_unique_value
fi

if [ -z "$OSPL_URI" ]; then
  export OSPL_URI="$_DEFAULT_OSPL_URI"
fi

export OSPL_HOME="$_OSPL_HOME_TO_USE"

unset _DEFAULT_OSPL_URI
unset _BUILDTIME_OSPL_HOME
unset _OSPL_HOME_TO_USE

export OSPL_INFOFILE="<stdout>"
export OSPL_ERRORFILE="<stderr>"
export OSPL_VERBOSITY=2
